import CombatBase from "@/Battle/CombatBase.js";

export class MachineBase extends CombatBase{
    constructor(){
        super();
        this.wait_event=false;
        this.state=null;
        this.handlers={};

        //-1向前，1向后
        this.leaveFlag=0;
        this.runing=true;
    }

    start=()=>{
        this.restore();
        this.switchState('start');
    }
    end=()=>{this.switchState('end');}

    update(){
        if(this.runing&&!this.wait_event){
            this.handlers[this.state]?.();
        }
    }

    

    /**
     * 
     * @param {number} flag
     * @description 
     * - 1向后退出
     * - -1向前退出 
     */
    _leave(flag){
        this.runing=!flag;
        this.leaveFlag=flag;
    }

    toback(){this._leave(-1);}
    isback(){return this.leaveFlag==-1;}
    tonext(){this._leave(1);}
    isnext(){return this.leaveFlag==1;}
    restore(){this._leave(0);}

    wait(){this.wait_event=true;}
    resume(){this.wait_event=false;}
    
    addStateHandler(state,handler){this.handlers[state]=handler;}

    switchState(state){
        this.resume();
        this.state=state;
    }

    clear(){this.handlers={};this._leave(0);}
}